<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handlecreateForm({})" v-auth="'btn:add:caiduan'">
          <Icon icon="fluent:add-12-filled" /> {{ t('发起裁断申请') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.projInfo?.projectname }}
        </a>
      </template>
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)" >
          <span style="text-decoration: underline;margin-right: 5px;">查看</span>
        </a>
        {{ record.oldid }} -->
        <a @click="handleDetailsForm(record)" >
          {{ record.oldid }}
        </a>
        
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <createForm @register="registercreateDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
    <component
      :is="inputFormComponent"
      v-model:visible="inputFormVisible"
      @register="registerDrawerBpmMyTask"
      @success="handleSuccess"
    />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsProcessCutPmProjectadjudicationcutList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent,ref,ComponentOptions,shallowRef, onMounted,watch } from 'vue';
  import { bpmTaskDefForm } from '/@/api/bpm/myTask';
  import { dynamicImport } from '/@/router/helper/routeHelper';
  import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  import qs from 'qs';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    pmProjectadjudicationcutDelete,
    pmProjectadjudicationcutListData,
  } from '/@/api/process/cut/pmProjectadjudicationcut';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import createForm from './createForm.vue';
  import { useUserStore } from '/@/store/modules/user';
  const userStore = useUserStore();

  const { t } = useI18n('process.cut.pmProjectadjudicationcut');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    // value: router.currentRoute.value.meta.title || t('裁断管理'),
    value: t('裁断管理'),
  };

  const props = defineProps({
    projectno: { type: String, default: '' },
  });

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('工程编号'),
        field: 'projInfo.projectno',
        component: 'Input',
      },
      {
        label: t('工程名称'),
        field: 'projInfo.projectname',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'status',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('裁断名称'),
        field: 'name',
        component: 'Input',
      },
      {
        label: t('申请时间'),
        field: 'createDate',
        component: 'DatePicker',
        componentProps: {
          format: 'YYYY-MM-DD HH:mm',
          showTime: { format: 'HH:mm' },
        },
        ifShow: false,
      },

      {
        label: t('选中的工程编号'),
        field: 'oldid',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('生成新项目的ID'),
        field: 'childid',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('裁断原因'),
        field: 'reasondetail',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('发起人'),
        field: 'promoter',
        component: 'Input',
        ifShow: false,
      },
      {
        label: t('是否审核'),
        field: 'ischeck',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
        ifShow: false,
      },
      {
        label: t('fileid'),
        field: 'fileid',
        component: 'Input',
        ifShow: false,
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
   
    {
      title: t('原工程编号'),
      dataIndex: 'oldid',
      key: 'a.oldid',
      sorter: true,
      width: 210,
      align: 'left',
      slot: 'projectno',
      
    },
    {
      title: t('原工程名称'),
      dataIndex: 'projInfo.projectname',
      key: 'proj1.projectname',
      sorter: true,
      width: 260,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('申请时间'),
      dataIndex: 'createDate',
      key: 'a.createtime',
      sorter: true,
      width: 120,
      align: 'center',
     
    },
    {
      title: t('裁断名称'),
      dataIndex: 'name',
      key: 'a.name',
      sorter: true,
      width: 130,
      align: 'left',
    },

    {
      title: t('裁断原因'),
      dataIndex: 'reasondetail',
      key: 'a.reasondetail',
      sorter: true,
      width: 130,
      align: 'left',
    },
    // {
    //   title: t('是否审批'),
    //   dataIndex: 'ischeck',
    //   key: 'a.ischeck',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'sys_yes_no',
    // },
    {
      title: t('新工程编号'),
      dataIndex: 'childid',
      key: 'a.childid',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('新工程名称'),
      dataIndex: 'newProjInfo.projectname',
      key: 'proj2.projectname',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('发起人'),
      dataIndex: 'createByName',
      key: 'a.promoter',
      sorter: true,
      width: 60,
      align: 'left',
    },
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 60,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
    {
      title: t('选中的工程编号'),
      dataIndex: 'oldid',
      key: 'a.oldid',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('生成新工程的ID'),
      dataIndex: 'childid',
      key: 'a.childid',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
    {
      title: t('fileid'),
      dataIndex: 'fileid',
      key: 'a.fileid',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: false,
    },
  ];

  const actionColumn: BasicColumn = {
    width: 80,
    align: 'left',
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑裁断'),
        onClick: handlecreateForm.bind(this, { id: record.id }),
        auth: 'btn:edit:caiduan',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除裁断'),
        popConfirm: {
          title: t('是否确认删除裁断'),
          confirm: handleDelete.bind(this, { id: record.id }),
        },
        auth: 'btn:delete:caiduan',
        ifShow: () => record.status == '9',
      },
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
      {
        icon: 'mdi:checkbox-marked-outline',
        title: t('任务办理'),
        onClick: handleBpmMyTask.bind(this, { id: record.id }),
        ifShow: () => record.status == '4' || record.status == '5' || record.status == '6',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registercreateDrawer, { openDrawer: opencreateDrawer }] = useDrawer();

  const [registerTable, { reload, getForm }] = useTable({
    api: pmProjectadjudicationcutListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.oldid,
        tabTitle: `项目详情(${record.oldid})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }
  function handlecreateForm(record: Recordable) {
    opencreateDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmProjectadjudicationcutDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
  const [registerDrawerBpmMyTask, { setDrawerData }] = useDrawer();
  const inputFormComponent = shallowRef<Nullable<any>>(null);
  const inputFormVisible = ref<Boolean>(false);

  async function handleBpmMyTask(record: Recordable) {
    const data = await bpmTaskDefForm({ formKey: 'Flow_ProjectAdjudicationCut', bizKey: record.id });
    if (data.result == 'true') {
      const url = data.pcUrl;
      const idx = url.indexOf('?');
      // component
      let component: ReturnType<typeof defineComponent>;
      const compStr = idx == -1 ? url : url.substring(0, idx);
      if (compStr && compStr != '') {
        const imp = dynamicImport(compStr);
        if (imp) component = createAsyncComponent(imp);
      }
      // params
      let params = {};
      const paramStr = idx == -1 ? '' : url.substring(idx + 1);
      if (paramStr && paramStr != '') {
        params = qs.parse(paramStr);
      }
      // open
      if (component) {
        inputFormComponent.value = component;
        inputFormVisible.value = true;
        setDrawerData(params);
      }
    } else {
      showMessage(data.message);
    }
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'Flow_ProjectAdjudicationCut', bizKey: record.id });
  }

  watch(
    () => props.projectno,
    () => {
      handleProjecte();
    },
  );

  onMounted(async () => {
    handleProjecte();
  });
  async function handleProjecte() {
    await getForm().setFieldsValue({
      'projInfo.projectno': props.projectno?props.projectno:userStore.getProjecte.code,
      status: '',
    });
    reload();
    await getForm().setFieldsValue({
      'projInfo.projectno': props.projectno?props.projectno:userStore.getProjecte.code,
      status: '',
    });
  }
</script>
